home *** CD-ROM | disk | FTP | other *** search
- Path: news.MWCI.NET!usenet
- From: stuffle@pcii.net
- Newsgroups: comp.lang.c++
- Subject: Re: Why Do I Use An Ampersand in Member Class Parameters?
- Date: 3 Feb 1996 16:48:57 GMT
- Organization: MidWest Communications, Inc.
- Message-ID: <4f03lq$gv7@hihat.mwci.net>
- References: <4emnv2$n5o@alcor.usc.edu>
- Reply-To: stuffle@pcii.net
- NNTP-Posting-Host: lan-pm1-5.pcii.net
- X-Newsreader: IBM NewsReader/2 v1.2.5
-
- In <4emnv2$n5o@alcor.usc.edu>, wawda@alcor.usc.edu (Abu Wawda) writes:
- >class Simple
- >{
- >public:
- > Simple();
- > int operator += (const Simple &);
- >private:
- > int data;
- >};
- >
- >I have seen many examples of this but I what I don't understand is why
- >there is an amersand after Simple? I would wind up implementating the
- >function as something like the following:
- >
-
- This is passing by reference, and it is similar to passing a pointer in so far that
- you are not passing the actual object, but its address.
-
- Passing by reference is safer than passing a pointer. You cannot do things like
- delete (or free) the memory, or reassign "parameter" to point to some other
- area of memory.
-
- I like to think of this as being similar to:
-
- function (VAR x : SomeType) return INTEGER; in Pascal.
-
- >int Simple::operator += (const Simple ¶meter)
- >{
- > data += parameter.data;
- >}
- >
- >I mean, I would understand if I parameter were a pointer to a Simple
- >class, but that is not the case, since I am not doing:
- >
- >int Simple::operator += (const Simple ¶meter)
- >{
- > data += parameter->data;
- >}
- >
- >Then why is there an amersand? I would appreciate any
- >suggestions. Thank you!
- >
- >
- >-Abu Wawda
- > wawda@scf.usc.edu
- >
- >
-
-
- \\\|///
- | ~ ~ |
- (- @ @ -)
- //------------------------oOOo----(_)----oOOo------------------------
- // Ken Sodemann (Stuffle) | Get Warped!!!!
- // Stuffle@PCII.NET |
- // http://users.mwci.net/~stuffle/ |
- //-------------------------------------------------------------------
-
-